/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.iecg.controller.app;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iecg.api.entity.IecgDeviceManage;
import com.iecg.api.entity.IecgEquip;
import com.iecg.api.entity.IecgUser;
import com.iecg.service.IecgDeviceManageService;
import com.iecg.service.IecgEquipService;
import com.iecg.service.IecgUserService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 设备管理
 *
 * @author pigx code generator
 * @date 2019-10-12 16:11:41
 */
@RestController
@AllArgsConstructor
@RequestMapping("app/device" )
@Api(value = "app_device", tags = "App设备管理")
public class AppDeviceManagerController {

    private final IecgDeviceManageService deviceManageService;

    private final IecgUserService iecgUserService;

    private final IecgEquipService iecgEquipService;

    /**
     * 通过userId查询健康档案
     * @param userId userId
     * @return R
     */
    @ApiOperation(value = "通过userId查询", notes = "通过userId查询")
    @GetMapping("/{userId}" )
    public R getByUserId(@PathVariable("userId") Long userId) {
        return R.ok(deviceManageService.list(Wrappers.<IecgDeviceManage>query()
				.lambda().eq(IecgDeviceManage::getUserId, userId)));
    }

	/**
	 * 新增设备管理
	 * @param deviceManager
	 * @return
	 */
	@ApiOperation(value = "新增设备管理", notes = "新增设备管理")
    @SysLog("新增设备管理" )
    @PostMapping
    public R save(@RequestBody IecgDeviceManage deviceManager) {
		PigxUser pigxUser = SecurityUtils.getUser();
		String username = pigxUser.getUsername();
		IecgUser iecgUser =iecgUserService.loadIecgUserByName(username);
		deviceManager.setUserId(iecgUser.getId());
        return R.ok(deviceManageService.save(deviceManager));
    }

	/**
	 * 更改设备在线状态
	 * @return
	 */
	@ApiOperation(value = "更改设备在线状态", notes = "更改设备在线状态")
	@SysLog("更改设备在线状态" )
	@PutMapping
	public R genOnline(@RequestBody IecgEquip iecgEquip){
		return R.ok(iecgEquipService.update(iecgEquip, Wrappers.<IecgEquip>lambdaUpdate().eq(IecgEquip::getPdid, iecgEquip.getPdid())));
	}

}
